
//校验登录函数
checkLogin()

// 点击退出按钮
document.querySelector('.logout').addEventListener('click', () => {
    // console.log(123)
    localStorage.removeItem('token')
    localStorage.removeItem('mobile')
    localStorage.removeItem('userId')
    const flag = confirm('你确定要退出吗？')

    document.querySelector('.nick_name').innerHTML = '未登录'
    document.querySelector('.mobile').innerHTML = '请先登录'

    document.querySelector('.logout').style.display = 'none'
    document.querySelector('.balance').innerText = '--'
    document.querySelector('.points').innerText = '--'
    document.querySelector('.coupon').innerText = '--'

})


//从本地存储取出用户名
// const mobile = localStorage.getItem('mobile')
const phoneNumber = localStorage.getItem('mobile')
const maskedPhoneNumber = phoneNumber.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
// console.log(maskedPhoneNumber);
if (localStorage.getItem('token')) {
    //渲染用户名
    document.querySelector('.mobile').innerHTML = maskedPhoneNumber
    document.querySelector('.nick_name').innerHTML = maskedPhoneNumber
}

if (localStorage.getItem('token')) {
    document.querySelector('.logout').style.display = 'block'
}

// 从服务器拿数据
// 渲染余额 积分 优惠券
async function balance() {
    const res = await axios({
        url: '/user/assets',
    })
    console.log(res.data.data.assets)
    document.querySelector('.balance').innerText = res.data.data.assets.balance
    document.querySelector('.points').innerText = res.data.data.assets.points
    document.querySelector('.coupon').innerText = res.data.data.assets.coupon
}
balance()


